import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "403",
        component: () => import("@/views/error/403.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },

  // {
  //   path: "/test",
  //   component: Layout,
  //   redirect: "/test/hello",
  //   name: "/test",
  //   meta: {
  //     title: "测试模块",
  //     icon: "api",
  //     hidden: false,
  //     alwaysShow: true,
  //     params: null,
  //   },
  //   children: [
  //     {
  //       path: "hello",
  //       name: "HelloWorld", //必须是组件名
  //       component: () => import("@/views/testpages/HelloWorld.vue"),
  //       meta: {
  //         title: "演示SVG图标使用",
  //         icon: "user",
  //         hidden: false,
  //         keepAlive: true,
  //         alwaysShow: false,
  //         params: null,
  //       },
  //     },
  //     {
  //       path: "TestEltable",
  //       name: "TestEltable5",
  //       component: () => import("@/views/testpages/TestEltable5.vue"),
  //       meta: {
  //         title: "TestEltable（测试）",
  //         icon: "user",
  //         hidden: false,
  //         keepAlive: true,
  //         alwaysShow: false,
  //         params: null,
  //       },
  //     },
  //     {
  //       path: "about",
  //       name: "About",
  //       component: () => import("@/views/testpages/About.vue"),
  //       meta: {
  //         title: "About（测试）",
  //         icon: "user",
  //         hidden: false,
  //         keepAlive: true,
  //         alwaysShow: false,
  //         params: null,
  //       },
  //     },
  //     {
  //       path: "viewexcel",
  //       name: "ViewEcel",
  //       component: () => import("@/views/testpages/ViewExcel.vue"),
  //       meta: {
  //         title: "Excel（测试）",
  //         icon: "user",
  //         hidden: false,
  //         keepAlive: true,
  //         alwaysShow: false,
  //         params: null,
  //       },
  //     },
  //     {
  //       path: "viewpdf",
  //       name: "ViewPdf",
  //       component: () => import("@/views/testpages/ViewPdf.vue"),
  //       meta: {
  //         title: "PDF（测试）",
  //         icon: "user",
  //         hidden: false,
  //         keepAlive: true,
  //         alwaysShow: false,
  //         params: null,
  //       },
  //     },
  //   ],
  // },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
